import instance from '@/utils/request'
import { GET_ARTICLE_LIST, GET_CHANNELLIST } from '../constants'

export const getChannelList = () => {
  return async (dispatch) => {
    const res = await instance.get('/channels')
    // console.log('频道', res)
    dispatch({
      type: GET_CHANNELLIST,
      payload: res.data.data.channels,
    })
  }
}

export const getArticleList = (params) => {
  return async (dispatch) => {
    const res = await instance.get('/mp/articles', {
      params,
    })
    // console.log('文章', res)
    dispatch({
      type: GET_ARTICLE_LIST,
      payload: res.data.data,
    })
  }
}

export const del = (id) => {
  return async () => {
    await instance.delete(`/mp/articles/${id}`)
  }
}

export const addArticle = (draft = false, data) => {
  return async () => {
    const res = await instance.post(`/mp/articles`, data, {
      params: {
        draft,
      },
    })
    console.log('添加文章', res)
  }
}

export const editArticle = (draft = false, data) => {
  return async () => {
    const res = await instance.put(`/mp/articles/${data.id}`, data, {
      params: {
        draft,
      },
    })
    console.log('修改文章', res)
  }
}

export const getAticleDetail = (id) => {
  return async () => {
    const res = await instance.get(`/mp/articles/${id}`)
    return res.data.data
  }
}
